import java.util.HashSet;
import java.util.Set;

/**
 * @author LKQ
 * @date 2021/12/10 21:10
 * @description 没看懂题目，答案一：使用哈希表
 */
public class Solution {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(3);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(0);
        ListNode l4 = new ListNode(-4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        Solution solution = new Solution();
        solution.hasCycle(l1);
   }

    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            if (!set.add(head)) {
                return true;
            }
            head = head.next;
        }
        return false;
    }

}
